package com.woniuxy.hotel.room.mapper;

import com.woniuxy.hotel.room.pojo.Room;
import com.woniuxy.hotel.room.pojo.RoomExample;
import com.woniuxy.hotel.room.util.PageInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoomMapper {
    @SelectProvider(type = RoomSqlProvider.class, method = "countByExample")
    int countByExample(RoomExample example);


    @DeleteProvider(type = RoomSqlProvider.class, method = "deleteByExample")
            int deleteByExample(RoomExample example);

    @Delete({
            "update room",
            "set roomFlag=1",
            "where roomId = #{roomid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer roomid);

    @Insert({
            "insert into room (roomId, hotelId, ",
            "roomNum, roomTypeId, ",
            "roomPrice, discount, ",
            "cashPledge, roomStatus, ",
            "idCard, roomTrait)",
            "values (#{roomid,jdbcType=INTEGER}, #{hotelid,jdbcType=INTEGER}, ",
            "#{roomnum,jdbcType=INTEGER}, #{roomtypeid,jdbcType=INTEGER}, ",
            "#{roomprice,jdbcType=DOUBLE}, #{discount,jdbcType=INTEGER}, ",
            "#{cashpledge,jdbcType=INTEGER}, #{roomstatus,jdbcType=INTEGER}, ",
            "#{idcard,jdbcType=INTEGER}, #{roomtrait,jdbcType=LONGVARCHAR})"
    })
    int insert(Room record);

    @InsertProvider(type = RoomSqlProvider.class, method = "insertSelective")
    int insertSelective(Room record);

    @SelectProvider(type = RoomSqlProvider.class, method = "selectByExampleWithBLOBs")
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "hotelId",property = "hotel",one=@One(select = "com.woniuxy.hotel.room.mapper.HotelMapper.selectByPrimaryKey")),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    List<Room> selectByExampleWithBLOBs(RoomExample example);

    @Select({
            "select",
            "*",
            "from room",
            "where roomFlag=0",
            "limit #{startIndex},#{limit} "
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomId", property = "imgs",
            many = @Many(select = "com.woniuxy.hotel.room.mapper.ImgMapper.selectByRoomlId")),
            @Result(column = "hotelId",property = "hotel",one=@One(select = "com.woniuxy.hotel.room.mapper.HotelMapper.selectByPrimaryKey")),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    List<Room> selectAllRoomsInfo(PageInfo pageInfo);

    @SelectProvider(type = RoomSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))

    })
    List<Room> selectByExample(RoomExample example);

    @Select({
            "select",
            "roomId, hotelId, roomNum, roomTypeId, roomPrice, discount, cashPledge, roomStatus, ",
            "idCard, roomTrait",
            "from room",
            "where roomId = #{roomid,jdbcType=INTEGER} and roomFlag=0"
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))

    })
    Room selectByPrimaryKey(Integer roomid);

    @Select({
            "select",
            "roomId, hotelId, roomNum, roomTypeId, roomPrice, discount, cashPledge, roomStatus, ",
            "idCard, roomTrait",
            "from room",
            "where roomId = #{roomid,jdbcType=INTEGER} and roomFlag=0"
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "hotelId",property = "hotel",one=@One(select = "com.woniuxy.hotel.room.mapper.HotelMapper.selectByPrimaryKey")),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))

    })
    Room selectWithHotelByPrimaryKey(Integer roomid);

    @Select({
            "select",
            "*",
            "from room",
            "where roomId = #{roomid,jdbcType=INTEGER} and roomFlag=0"
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomId", property = "imgs",
                    many = @Many(select = "com.woniuxy.hotel.room.mapper.ImgMapper.selectByRoomlId")),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    Room selectWithImgByPrimaryKey(Integer roomid);

    /**
     * 依据酒店id查询所有房间
     *
     * @param hotelId 酒店id
     * @return
     */
    @Select({
            "select",
            "roomId, hotelId, roomNum, roomTypeId, roomPrice, discount, cashPledge, roomStatus, ",
            "idCard, roomTrait",
            "from room",
            "where hotelId = #{hotelId,jdbcType=INTEGER} and roomFlag=0"
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    List<Room> selectByHotelId(Integer hotelId);

    @Select({
            "select",
            "roomId, hotelId, roomNum, roomTypeId, roomPrice, discount, cashPledge, roomStatus, ",
            "idCard, roomTrait",
            "from room",
            "where roomTypeId = #{typeId,jdbcType=INTEGER} and roomFlag=0"
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    List<Room> selectByTypelId(Integer typeId);

    @UpdateProvider(type = RoomSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Room record, @Param("example") RoomExample example);

    @UpdateProvider(type = RoomSqlProvider.class, method = "updateByExampleWithBLOBs")
    int updateByExampleWithBLOBs(@Param("record") Room record, @Param("example") RoomExample example);

    @UpdateProvider(type = RoomSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") Room record, @Param("example") RoomExample example);

    @UpdateProvider(type = RoomSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Room record);

    @Update({
            "update room",
            "set hotelId = #{hotelid,jdbcType=INTEGER},",
            "roomNum = #{roomnum,jdbcType=INTEGER},",
            "roomTypeId = #{roomtypeid,jdbcType=INTEGER},",
            "roomPrice = #{roomprice,jdbcType=DOUBLE},",
            "discount = #{discount,jdbcType=INTEGER},",
            "cashPledge = #{cashpledge,jdbcType=INTEGER},",
            "roomStatus = #{roomstatus,jdbcType=INTEGER},",
            "idCard = #{idcard,jdbcType=INTEGER},",
            "roomTrait = #{roomtrait,jdbcType=LONGVARCHAR}",
            "where roomId = #{roomid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKeyWithBLOBs(Room record);

    /**
     * 依房间类型修改价格
     * @param record 实体类
     * @return
     */
    @Update({
            "update room",
            "set roomPrice = #{roomprice,jdbcType=DOUBLE}",
            "where roomTypeId = #{roomtypeid,jdbcType=INTEGER}"
    })
    int updateByRoomTypeId(Room record);

    @Update({
            "update room",
            "set hotelId = #{hotelid,jdbcType=INTEGER},",
            "roomNum = #{roomnum,jdbcType=INTEGER},",
            "roomTypeId = #{roomtypeid,jdbcType=INTEGER},",
            "roomPrice = #{roomprice,jdbcType=DOUBLE},",
            "discount = #{discount,jdbcType=INTEGER},",
            "cashPledge = #{cashpledge,jdbcType=INTEGER},",
            "roomStatus = #{roomstatus,jdbcType=INTEGER},",
            "idCard = #{idcard,jdbcType=INTEGER}",
            "where roomId = #{roomid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Room record);

    /**
     * 恢复指定房间
     * @param roomId 要恢复的房间id
     * @return
     */
    @Update({
            "update room",
            "set roomFlag=0",
            "where roomId=#{roomId,jdbcType=INTEGER}"
    })
    int recoverByRoomId(Integer roomId);


  //展示所有房间
    @Select("select roomId, hotelId, roomNum, roomTypeId, roomPrice, discount, cashPledge, roomStatus from room")
    public List<Room> findAllRoom();

    @Update({
            "update room",
            "set roomFlag=1",
            "where hotelId=#{hotelId,jdbcType=INTEGER}"
    })
    int deleteByHotelId(Integer hotelId);


    /**
     * 查询已删除的房间信息
     * @param pageInfo 分页信息
     * @return
     */
    @Select({
            "select",
            "*",
            "from room",
            "where roomFlag!=0",
            "limit #{startIndex},#{limit} "
    })
    @Results({
            @Result(column = "roomId", property = "roomid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "hotelId", property = "hotelid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomNum", property = "roomnum", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTypeId", property = "roomtypeid", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomPrice", property = "roomprice", jdbcType = JdbcType.DOUBLE),
            @Result(column = "discount", property = "discount", jdbcType = JdbcType.INTEGER),
            @Result(column = "cashPledge", property = "cashpledge", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomStatus", property = "roomstatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "idCard", property = "idcard", jdbcType = JdbcType.INTEGER),
            @Result(column = "roomTrait", property = "roomtrait", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "roomId", property = "imgs",
                    many = @Many(select = "com.woniuxy.hotel.room.mapper.ImgMapper.selectByRoomlId")),
            @Result(column = "hotelId",property = "hotel",one=@One(select = "com.woniuxy.hotel.room.mapper.HotelMapper.selectByPrimaryKey")),
            @Result(column = "roomTypeId",property = "roomType",one = @One(select = "com.woniuxy.hotel.room.mapper.RoomTypeMapper.selectByPrimaryKey"))
    })
    List<Room> selectDeletedRoomsInfo(PageInfo pageInfo);









}



